//
//  FCAlertToastView.m
//  webViewLocal
//
//  Created by WJC on 2021/6/23.
//

#import "FCAlertToastView.h"
#import "WJCDefine.h"
@interface FCAlertToastView ()
@property (nonatomic,strong) UILabel *contentLabel;
@property (nonatomic,strong) UIView *contentView;
@end
@implementation FCAlertToastView
+(void)showSuccessWithStatus:(NSString *)status andInputEnable:(BOOL)inputEnable{

    if (inputEnable) {
        FCAlertToastView *toast = [FCAlertToastView sharedCoverView];
        [toast setCoverTitle:status andColor:HEXC(0x00C158)];
        [FCAlertToastView showWithToast:toast Status:status];
    }else{
        FCAlertToastView *toast = [FCAlertToastView sharedToastView];
        [toast setTitle:status andColor:HEXC(0x00C158)];
        [FCAlertToastView showWithToast:toast Status:status];
    }
   
}
+(void) showFailWithStatus:(NSString *)status andInputEnable:(BOOL)inputEnable{
    static  FCAlertToastView *toastView;
    [SVProgressHUD dismiss];
    if (inputEnable) {
        FCAlertToastView *toast = [FCAlertToastView sharedCoverView];
        [toast setCoverTitle:status andColor:HEXC(0xE67373)];
        [FCAlertToastView showWithToast:toast Status:status];
    }else{
        FCAlertToastView *toast = [FCAlertToastView sharedToastView];
        [toast setTitle:status andColor:HEXC(0xE67373)];
        [FCAlertToastView showWithToast:toast Status:status];
    }
}
+ (instancetype)sharedToastView
{
    static FCAlertToastView *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[FCAlertToastView alloc] initWithTitle:@"" andColor:HEXC(0x6291F6)];
    });
    return instance;
}
+ (instancetype)sharedCoverView
{
    static FCAlertToastView *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[FCAlertToastView alloc] initWithCoverTitle:@"" andColor:HEXC(0x6291F6)];
    });
    return instance;
}
+(void)showWithToast:(FCAlertToastView *)toastView Status:(NSString*)title{
    if(toastView.superview){
        return;
    }
    NSInteger time = title.length*0.05;
    if (time<2) {
        time = 2;
    }
    if (time>10) {
        time = 10;
    }
    [[UIApplication sharedApplication].keyWindow addSubview:toastView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [toastView removeFromSuperview];
    });
}


-(instancetype)initWithTitle:(NSString*)title andColor:(UIColor*)color{
    if (self = [super init]) {
        
        self.frame = CGRectMake(40, WJCStatusBarHeight+30, WJCScreenW-80, 54);
        WJCSETLRViewBorderRadius(self, 27);
        createCommonLabel(contentLabel, font15, [UIColor whiteColor]);
        contentLabel.numberOfLines = 0;
        
        [self addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
    }
    
    return self;
}
-(void)setTitle:(NSString*)title andColor:(UIColor*)color{
    self.backgroundColor = color;
    self.contentLabel.frame = CGRectMake(30, 0, WJCScreenW-80-60, 99);
    self.contentLabel.text = title;
    [self.contentLabel sizeToFit];
    if (self.contentLabel.height+32>54) {
        self.height =self.contentLabel.height+32;
    }else{
        self.height = 54;
    }
    self.contentLabel.centerX = (WJCScreenW-80)*0.5;
    self.contentLabel.centerY = self.height*0.5;
}
-(instancetype)initWithCoverTitle:(NSString*)title andColor:(UIColor*)color{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, WJCScreenW, WJCScreenH);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.15];
        createLineView(contentView);
        [self addSubview:contentView];
        contentView.frame = CGRectMake(40, WJCStatusBarHeight+30, WJCScreenW-80, 54);
        self.contentView = contentView;
        WJCSETLRViewBorderRadius(contentView, 27);
        createCommonLabel(contentLabel, font15, [UIColor whiteColor]);
        contentLabel.numberOfLines = 0;
        
        [contentView addSubview:contentLabel];
        self.contentLabel = contentLabel;
    }
    
    return self;
}
-(void)setCoverTitle:(NSString*)title andColor:(UIColor*)color{
    self.contentView.backgroundColor = color;
    self.contentLabel.frame = CGRectMake(30, 0, WJCScreenW-80-60, 99);
    self.contentLabel.text = title;
    [self.contentLabel sizeToFit];
    if (self.contentLabel.height+32>54) {
        self.contentView.height =self.contentLabel.height+32;
    }else{
        self.contentView.height = 54;
    }
    self.contentLabel.centerX = (WJCScreenW-80)*0.5;
    self.contentLabel.centerY = self.contentView.height*0.5;
}
@end
